package com.myapplication.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.TextView;

import com.bumptech.glide.BitmapRequestBuilder;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.model.GlideUrl;
import com.myapplication.model.Footer;
import com.myapplication.model.GoodsList;
import com.myapplication.zhou.alldemo.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhoushikun on 2016/7/1.
 */
public class SearchPageRecycleViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<Object> goodsLists = new ArrayList<>();
    private Context context;
    private LayoutInflater inflater;
    public static int TYPE_LIST = 0;
    public static int TYPE_FOOTER = 1;
    public SearchClick searchClick = null;
    public BitmapRequestBuilder<GlideUrl,Bitmap> requestBuilder;
    public SearchPageRecycleViewAdapter(Context context, List<Object> goodsLists) {
        this.context = context;
        this.goodsLists = goodsLists;
        this.inflater = LayoutInflater.from(context);
        requestBuilder = Glide.with(context)
                .from(GlideUrl.class)
                .asBitmap()
                .dontAnimate()
                .centerCrop()
                .error(R.mipmap.wutu)
                .diskCacheStrategy(DiskCacheStrategy.ALL);
    }

    public void setOnItemClick(SearchClick click) {
        this.searchClick = click;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_LIST) {
            View view = inflater.inflate(R.layout.search_page_recycle_view, parent, false);
            SearchViewHolder searchViewHolder = new SearchViewHolder(view, searchClick);
            return searchViewHolder;
        } else if (viewType == TYPE_FOOTER) {
            View view = inflater.inflate(R.layout.recycler_footer, parent, false);
            FooterViewHolder footerViewHolder = new FooterViewHolder(view);
            return footerViewHolder;
        }
        return null;
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof SearchViewHolder) {
            SearchViewHolder holderSearch = (SearchViewHolder) holder;
            GoodsList goodsList = (GoodsList) goodsLists.get(position);
            holderSearch.bindView(goodsList);
          /*  Glide.with(context).load(goodsList.getGoods_image()).crossFade().error(R.mipmap.wutu).into(holderSearch.imageView);*/
            requestBuilder.load(new GlideUrl("http://h.hiphotos.baidu.com/zhidao/pic/item/d6ca7bcb0a46f21fc1fcaef4f6246b600d33ae49.jpg")).into(holderSearch.imageView);

            holderSearch.textViewgoodname.setText(goodsList.getGoods_name());
            holderSearch.textViewpingjianumber.setText(goodsList.getEvaluation_count());
            holderSearch.ratingbar.setRating(Float.parseFloat(goodsList.getEvaluation_good_star()));
            holderSearch.textViewprice.setText(goodsList.getGoods_price());
        } else if (holder instanceof FooterViewHolder) {
            FooterViewHolder footerViewHolder = (FooterViewHolder) holder;
            Footer footer = (Footer) goodsLists.get(position);
            footerViewHolder.textView_progress.setText(footer.getText());
        }
    }

    public void update(List<Object> goodsLists) {
        this.goodsLists = goodsLists;
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        if (goodsLists.get(position) instanceof GoodsList) {
            return TYPE_LIST;
        } else if (goodsLists.get(position) instanceof Footer)
            return TYPE_FOOTER;

        return -1;
    }

    @Override
    public int getItemCount() {
        return goodsLists.size();
    }

    public class SearchViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private ImageView imageView;
        private TextView textViewgoodname;
        private TextView textViewprice;
        private RatingBar ratingbar;
        private TextView textViewpingjianumber;
        private LinearLayout layout;
        private SearchClick click;

        public SearchViewHolder(View itemView, SearchClick click) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.imageView);
            textViewgoodname = (TextView) itemView.findViewById(R.id.textView_good_name);
            textViewprice = (TextView) itemView.findViewById(R.id.textView_price);
            ratingbar = (RatingBar) itemView.findViewById(R.id.rating_bar);
            textViewpingjianumber = (TextView) itemView.findViewById(R.id.textView_pingjia_number);
            layout = (LinearLayout) itemView.findViewById(R.id.layout);
            this.click = click;
            layout.setOnClickListener(this);
        }

        public void bindView(Object object) {
            layout.setTag(object);
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.layout:
                    click.onSearchClick(v, v.getTag(), getLayoutPosition());
                    break;
            }
        }
    }

    public interface SearchClick {
        void onSearchClick(View v, Object object, int position);
    }

    public class FooterViewHolder extends RecyclerView.ViewHolder {
        public ProgressBar progressBar;
        public TextView textView_progress;

        public FooterViewHolder(View itemView) {
            super(itemView);
            progressBar = (ProgressBar) itemView.findViewById(R.id.progressBar);
            textView_progress = (TextView) itemView.findViewById(R.id.textView_progress);
        }
    }
}
